﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NhanDang
{
    public partial class frmLearning : Form
    {
        PatternRecognition obj;
        public frmLearning()
        {
            InitializeComponent();
            obj = new PatternRecognition(192, 160, 7, 0.1);
        }
        int value1;
        int value2;
        public void Form1_Load(object sender, EventArgs e)
        {
            //trackBar2.Value = 5;
            //trackBar1.Value = 15;
            value1 = 15;
            value2 = 5;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
        Bitmap bitMap;
        Bitmap bit;
        bool flag = true;
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            getDataInput(openFileDialog1.FileName);
            flag = false;
        }

        private void getDataInput(string file)
        {
            Image img = Image.FromFile(file);
            bitMap = new Bitmap(img);
            //pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, 245);


            //pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, (byte)trackBar1.Value);
            //bit = ImageProcessing.ResizeImage(ImageProcessing.ToBinaryLocBits(bitMap, (byte)trackBar1.Value));

            pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, (byte)value1);
            bit = ImageProcessing.ResizeImage(ImageProcessing.ToBinaryLocBits(bitMap, (byte)value1));

            //Bitmap bitshow = ImageProcessing.ArrayToBitMap(ImageProcessing.arrInput(bit, 16, 12, trackBar2.Value), 12, 16);
            Bitmap bitshow = ImageProcessing.ArrayToBitMap(ImageProcessing.arrInput(bit, 16, 12, value2), 12, 16);

            double[] arr = ImageProcessing.arrInput(bit, 16, 12, value2);
            panel1.Controls.Clear(); //Hiển thị hình ảnh sau khi đã chuẩn hóa
            for (int i = 0; i < bitshow.Height; i++)
            {
                for (int j = 0; j < bitshow.Width; j++)
                {
                    Panel pn = new Panel
                    {
                        Size = new Size(10, 10),
                        Name = "pn" + i + j,
                        Location = new Point(j * 10, i * 10),
                        BackColor = bitshow.GetPixel(j, i)
                    };
                    panel1.Controls.Add(pn);
                }
            }
            obj.ArrDataInput = ImageProcessing.arrInput(bit, 16, 12, value2);
        }
        //private void trackBar1_Scroll(object sender, EventArgs e)
        //{
        //    label1.Text = trackBar1.Value.ToString();
        //    pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, (byte)trackBar1.Value);
        //    bit = ImageProcessing.ResizeImage(ImageProcessing.ToBinaryLocBits(bitMap, (byte)trackBar1.Value));
        //    pictureBox1.Image = bit;
        //}

        //private void trackBar2_Scroll(object sender, EventArgs e)
        //{
        //    label2.Text = trackBar2.Value.ToString();
        //    Bitmap bitshow = ImageProcessing.ArrayToBitMap(ImageProcessing.arrInput(bit, 16, 12, trackBar2.Value), 12, 16);
        //    double[] arr = ImageProcessing.arrInput(bit, 16, 12, trackBar2.Value);
        //    panel1.Controls.Clear();
        //    for (int i = 0; i < bitshow.Height; i++)
        //    {
        //        for (int j = 0; j < bitshow.Width; j++)
        //        {
        //            Panel pn = new Panel
        //            {
        //                Size = new Size(10, 10),
        //                Name = "pn" + i + j,
        //                Location = new Point(j * 10, i * 10),
        //                BackColor = bitshow.GetPixel(j, i)
        //            };
        //            panel1.Controls.Add(pn);
        //        }
        //    }
        //    obj.ArrDataInput = ImageProcessing.arrInput(bit, 16, 12, trackBar2.Value);
        //    obj.ArrDesiredOuput = DataProvider.desiredOutput(comboBox1.SelectedIndex, 7);
        //}

        //private void button2_Click(object sender, EventArgs e) //nhận dạng
        //{
        //    double min = double.MaxValue;
        //    FileInfo f = new FileInfo(Application.StartupPath + "\\Data\\");
        //    string re = "";
        //    foreach (FileInfo item in f.Directory.GetFiles())
        //    {
        //        double[][,] arrData = DataProvider.ReadData(item.Name.Split('.')[0], 192, 7, 160);
        //        double[,] arrWInput = arrData[0];
        //        double[,] arrWOutput = arrData[1];
        //        double[] ValueOutput = new double[7];
        //        for (int i = 0; i < 7; i++)
        //        {
        //            ValueOutput[i] = arrData[2][0, i];
        //        }
        //        PatternRecognition pat = new PatternRecognition(192, 160, 7, ImageProcessing.arrInput(bit, 16, 12, value2), ValueOutput, arrWInput, arrWOutput, 0.1);
        //        double ss = pat.FindNumberError();

        //        if (ss < min)
        //        {
        //            min = ss;
        //            re = item.Name;
        //        }
        //    }
        //    MessageBox.Show(DataProvider.Name(re));
        //}

        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn hình cần học");
                comboBox1.Focus();
                return;
            }
            if (flag)
            {
                MessageBox.Show("Bạn chưa chọn file ảnh nào");
                return;
            }
            obj.ArrDesiredOuput = DataProvider.desiredOutput(comboBox1.SelectedIndex, 7);

            string fileName = "";
            double[] arr = DataProvider.desiredOutput(comboBox1.SelectedIndex, 7);
            for (int i = 0; i < arr.Length; i++)
            {
                fileName += arr[i].ToString();
            }
            int cou = 1;
            foreach (string item in openFileDialog1.FileNames)
            {
                toolStripStatusLabel1.Text = "Ảnh đang học: " + cou++ + " of " + openFileDialog1.FileNames.Length;
                getDataInput(item);
                this.Refresh();
                if (DataProvider.CheckFileExists(fileName))
                {
                    double[][,] arrData = DataProvider.ReadData(fileName, 192, 7, 160);
                    double[,] arrWInput = arrData[0];
                    double[,] arrWOutput = arrData[1];
                    obj.ArrWInput = arrWInput;
                    obj.ArrWOutput = arrWOutput;
                }
                else
                {
                    obj.InitializeW();
                }
                obj.NeuralLearning(progressBar1);
            }
            toolStripStatusLabel1.Text = "Đã học xong";
            MessageBox.Show("Học thành công");
        }
    }
}
